package com.niodata.dp.task.dao;

import com.niodata.dp.task.entity.TaskInfo;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface TaskMapper {

  @Insert(
      "insert into TASK_INFO(OWNER_ID,OWNER_TYPE,TASK_ID,AZ_ID,SCHEDULED_TIME,CREATE_TIME,"
          + "MODIFY_TIME,"
          + "CREATOR,SCHEDULED,CRON,TASK_NAME,DESCRIPTION,BIND_EXECUTOR,EXECUTOR_ID,PARAMS,APP_ID,"
          + "APP_NAME,OPTIONS,IS_TEMPLATE,ALERT_SETTING,DEL_PROTECTION)"
          + "values(#{task.ownerId},#{task.ownerType},#{task.taskId},#{task.azProjectId},"
          + "#{task.scheduledTime},#{task.createTime},#{task.modifyTime},"
          + "#{task.creator},#{task.scheduled},"
          + "#{task.scheduleCron},#{task.name},#{task.description},"
          + "#{task.bindExecutor},#{task.executorId},#{task.params},#{task.appId},"
          + "#{task.appName},#{task.options},#{task.template},#{task.alertSetting},"
          + "#{task.delProtection})")
  public void saveTaskInfo(@Param("task") TaskInfo taskInfo);

  @Update("update TASK_INFO set AZ_ID=#{task.azProjectId},MODIFY_TIME=#{task.modifyTime},"
      + "SCHEDULED_TIME=#{task.scheduledTime},CREATE_TIME=#{task.createTime},"
      + "CREATOR=#{task.creator},SCHEDULED=#{task.scheduled},BIND_EXECUTOR=#{task.bindExecutor},"
      + "EXECUTOR_ID=#{task.executorId},CRON=#{task.scheduleCron},"
      + "DESCRIPTION=#{task.description},PARAMS=#{task.params},OPTIONS=#{task.options},"
      + "ALERT_SETTING=#{task.alertSetting},DEL_PROTECTION=#{task.delProtection},"
      + "APP_ID=#{task.appId} "
      + "where TASK_ID=#{task.taskId}")
  public void updateTaskInfo(@Param("task") TaskInfo taskInfo);

  @Delete("delete from TASK_INFO where TASK_ID=#{taskId}")
  public void deleteTask(@Param("taskId") String taskId);

  @Select("select * from TASK_INFO where GROUP_ID=#{groupId} and IS_TEMPLATE=0 "
      + "order by MODIFY_TIME desc limit #{offset},#{count} ")
  @ResultMap("taskInfoResultMap")
  public List<TaskInfo> getGroupTasks(@Param("groupId") long groupId, @Param("offset") int offset,
      @Param("count") int count);

  @Select("select * from TASK_INFO where GROUP_ID=#{groupId} and IS_TEMPLATE=0")
  @ResultMap("taskInfoResultMap")
  public List<TaskInfo> getGroupAllTasks(@Param("groupId") long groupId);

  @Select("select * from TASK_INFO where TASK_ID=#{taskId}")
  @ResultMap("taskInfoResultMap")
  public TaskInfo getTaskById(@Param("taskId") String taskId);

  @Select("select * from TASK_INFO where TASK_NAME=#{name}")
  @ResultMap("taskInfoResultMap")
  public TaskInfo getTaskByName(@Param("name") String name);

  @Select("select * from TASK_INFO where IS_TEMPLATE=0")
  @ResultMap("taskInfoResultMap")
  public List<TaskInfo> getAllTasks();

  @Select("select case when exists (select 1 from TASK_INFO"
      + " where APP_ID=#{appId}) then 1 else 0 end")
  public boolean existsTaskInApp(@Param("appId") String appId);


  /**
   * 查询用户可见的全部task，包括个人、组和公开的.
   *
   * @param userId userid
   * @param ids ids
   * @param keyWord name or desc contains key word
   * @param offset offset
   * @param count limit count
   * @param scheduled scheduled
   * @return list
   */
  public List<TaskInfo> findUserAuthorizedTasks(@Param("userId") Long userId,
      @Param("appId") String appId,
      @Param("ids") List<Long> ids,
      @Param("keyWord") String keyWord,
      @Param("offset") int offset,
      @Param("count") int count,
      @Param("scheduled") String scheduled);

  /**
   * 查询用户自己的任务.
   *
   * @param userId userid
   * @param keyWord name or desc contains key word
   * @param offset offset
   * @param count limit count
   * @param scheduled scheduled 已调度 ，noscheduled 未调度， 空 所有流程
   * @return list
   */
  public List<TaskInfo> findUserTasks(@Param("userId") Long userId,
      @Param("appId") String appId,
      @Param("keyWord") String keyWord,
      @Param("offset") int offset,
      @Param("count") int count,
      @Param("scheduled") String scheduled);

  /**
   * 查询某个组下可见的task.
   *
   * @param userId userid
   * @param groupId groupid
   * @param keyWord name or desc contains key word
   * @param offset offset count
   * @param count count limit
   * @param scheduled scheduled 已调度 ，noscheduled 未调度， 空 所有流程
   * @return list
   */
  public List<TaskInfo> findUserAuthorizedGroupTasks(@Param("userId") Long userId,
      @Param("groupId") Long groupId,
      @Param("appId") String appId,
      @Param("keyWord") String keyWord,
      @Param("offset") int offset,
      @Param("count") int count,
      @Param("scheduled") String scheduled);

  @ResultMap("taskInfoResultMap")
  public List<TaskInfo> getTaskByNameOrDescContains(@Param("contains") String contains);

  public int getGroupTaskCount(@Param("ownerId") Long ownerId,
      @Param("ids") List<Long> ids, @Param("scheduled") Integer scheduled);

}
